import 'package:flutter/material.dart';

import 'target_page.dart';

class ImageScanWidget extends StatefulWidget {
  final List<String> list;
  ImageScanWidget(this.list);
  @override
  _ImageScanWidgetState createState() => _ImageScanWidgetState();
}

class _ImageScanWidgetState extends State<ImageScanWidget> {
  int currentIndex = 0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.blueGrey.withOpacity(0.5),
      body: Container(
        width: double.infinity,
        height: double.infinity,
        child: Stack(
          alignment: Alignment.center,
          children: [
            Listener(
                onPointerUp: (PointerUpEvent event) {
                  if (currentIndex == widget.list.length) {
                    // Navigator.of(context).pop();
                    Navigator.of(context).pushReplacement(MaterialPageRoute(
                        builder: (BuildContext context) => TargetPage()));
                  }
                },
                child: buildPageView()),
            buildDot(),
          ],
        ),
      ),
    );
  }

  Widget buildDot() {
    if (currentIndex == widget.list.length) {
      return Container();
    }
    return Positioned(
      bottom: 60,
      right: 60,
      child: Container(
        width: 60,
        height: 30,
        alignment: Alignment.center,
        decoration: BoxDecoration(
          color: Colors.blue.withOpacity(0.5),
          borderRadius: BorderRadius.all(Radius.circular(15)),
          border: Border.all(color: Colors.white),
        ),
        child: Text(
          "${currentIndex + 1}/${widget.list.length}",
          style: TextStyle(
              fontSize: 16,
              color: Colors.white,
              decoration: TextDecoration.none),
        ),
      ),
    );
  }

  PageView buildPageView() {
    return PageView.builder(
      onPageChanged: (value) {
        setState(() {
          currentIndex = value;
        });
      },
      itemCount: widget.list.length + 1,
      itemBuilder: (BuildContext context, int index) {
        if (index == widget.list.length) {
          return TargetPage();
        }
        return Image.network(widget.list[index]);
      },
    );
  }
}
